<?php
/**
 * 日志偷懒函数
 * @author jc
 *
 * Class LogHelper
 * @package helper
 *
 */

namespace app\common\helper;

use think\Exception;
use think\facade\Log;

class LogHelper {

    /*是否已经初始化*/
    private static $_is_init = false;

    /*此段调试的标签*/
    private static $_tag = null;

    /**
     * 标记调试开始点和时间，初始化标记tag
     *
     * @param $tag
     * @throws Exception
     */
    public static function start($tag) {
        if ($tag === null) {
            throw new Exception('请传递调试的标签，便于日志查看辨识');
        }
        static::$_tag       = $tag;
        static::$_is_init   = true;

        Log::info(static::$_tag . ': start_time:' . date('Y-m-d H:i:s', time()));
    }

    /**
     * 调试详情
     *
     * @param $message
     * @param null $tag
     * @throws Exception
     */
    public static function info($message, $tag = null) {
        if (!static::$_is_init) {
            static::start($tag);
        }

        Log::info(static::$_tag . ': ' . $message);
    }
    
    /**
     * 调试结束，清除标签
     *
     * @throws Exception
     */
    public static function end() {
        if (!static::$_is_init) {
            throw new Exception('请调用start或者info方法先初始化');
        }
        Log::info(static::$_tag . ': end_time:' . date('Y-m-d H:i:s', time()));

        static::$_is_init   = false;
        static::$_tag       = null;
    }
}